package com.douhao.personal.controller;

import com.douhao.personal.model.Permission;
import com.douhao.personal.model.RolePermissionBo;
import com.douhao.personal.service.PermissionService;
import com.douhao.personal.util.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/permit")
public class PermissionController extends BaseController {

    @Resource
    PermissionService permissionService;

    /**
     * 权限列表
     */
    @RequestMapping(value = "permissionList")
    public ModelAndView index(String pageNo, Permission permission) {
        Page<Permission> roleList = permissionService.pageList(permission, pageNo == null ? 1 : Integer.valueOf(pageNo), pageSize);
        ModelAndView modelAndView = new ModelAndView("permit/permissionList", "page", roleList);
        modelAndView.addObject("permission", permission);
        return modelAndView;
    }

    /**
     * 权限删除
     * @param ids
     */
    @RequestMapping(value="deletePermissionById",method=RequestMethod.POST)
    public Map<String,Object> deletePermissionById(String ids){
        return permissionService.deletePermissionById(ids);
    }



    /**
     * 权限添加
     * @param permission
     * @return
     */
    @RequestMapping(value="addPermission",method=RequestMethod.POST)
    public Map<String,Object> addPermission(Permission permission){
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            int count = permissionService.insert(permission);
            resultMap.put("status", 200);
            resultMap.put("successCount", count);
        } catch (Exception e) {
            resultMap.put("status", 500);
            resultMap.put("message", "添加失败，请刷新后再试！");
        }
        return resultMap;
    }

    /**
     * 权限分配
     */
    @RequestMapping(value = "/permissionBoList")
    public ModelAndView permissionBoList(String pageNo,RolePermissionBo rolePermissionBo){
        Page<RolePermissionBo> permissionList = permissionService.findRoleAndPermission(rolePermissionBo, pageNo==null?1:Integer.valueOf(pageNo), pageSize);
        ModelAndView modelAndView = new ModelAndView("permit/rolePermissionBoList", "page", permissionList);
        modelAndView.addObject("rolePermissionBo",rolePermissionBo);
        return modelAndView;
    }


    /**
     *查询角色拥有的权限
     */
    @RequestMapping(value = "/selectPermissionByRoleId")
    public List<RolePermissionBo> selectPermissionByRoleId(String id){
        List<RolePermissionBo> rpb = permissionService.selectPermissionByRoleId(id);
        return rpb;
    }

    /**
     * 保存勾选的权限
     */
    @RequestMapping(value="addPermission2Role")
    public Map<String,Object> addPermission2Role(String roleId,String ids){
        return permissionService.addPermission2Role(roleId,ids);
    }

}
